<?php
class paper_model extends CI_Model {
	private $table_name = "paper";
	
	function __construct(){
	  parent::__construct();
	}
 
	function get_papers($num, $offset, $cond) {
    		#$query = $this->db->get('paper', $num, $offset);        
		$offset = ($offset == 1 ? 0 : $offset);
		$sql = "select file_id,file_name,school.name as school_name,profession.name as pro_name,level.name as level_name,words,teacher,count,lock_state,use_school,download_user,upload_user,upload_time,download_time,lock_time from (select * from paper $cond order by paper.upload_time desc limit $offset,$num) paper left join level on paper.level_id=level.id left join profession on paper.profession_id=profession.id left join school on paper.school_id=school.id";
    		$query = $this->db->query($sql); 
    	log_message('debug', __FUNCTION__.':sql='.$sql);
	  	return $query;
   	}

   	function get_papers_num($cond) {
    		#$query = $this->db->get('paper', $num, $offset);        
		$sql = "select count(file_id) as paper_num  from (select * from paper $cond) paper left join level on paper.level_id=level.id left join profession on paper.profession_id=profession.id left join school on paper.school_id=school.id";
    		$query = $this->db->query($sql);  
    		$obj = $query->row_object();      
	  	if($obj) return $obj->paper_num;
	  	return 0;
   	}

	function get_paper_from_id($id) {
		$sql = "select file_name,lock_state,download_user,upload_user from paper where file_id=".$id;
		$query = $this->db->query($sql);
		return $query;
	}

 	function get_lock_statue($id) {
        $sql = "select lock_state from paper where file_id=".$id;
        $query = $this->db->query($sql);
        return $query;
    }

 	function get_file_md5($id) {
        $sql = "select file_md5 from paper where file_id=".$id;
        $query = $this->db->query($sql);
        return $query;
    }

    function get_file_md5s() {      
        $md5s = $this->db->select("file_md5")->from('paper')->get()->result();
        $ret = array();
        if($md5s){
        	foreach ($md5s as $value) {
        		array_push($ret, $value->file_md5);
        	}
        	log_message("debug", "get_file_md5s: ret=".json_encode($ret));
        	return $ret;
        }
        return false;
    }

    function get_file_name($id) {
        $sql = "select file_name from paper where file_id=".$id;
        $query = $this->db->query($sql);
        return $query;
    }

	function get_school() {
		$sql = "select id,name from school";
		$query = $this->db->query($sql);
		return $query;
	}

	function get_school_by_id($name) {
		$sql = "select id,name from school where name = '".$name."'";
		$query = $this->db->query($sql);
		return $query->row_object();
	}

	function set_school($data) {
		return $this->db->insert('school', $data);
	}

	function get_profession() {
		$sql = "select id,name from profession";
		$query = $this->db->query($sql);
		return $query;
	}

	function get_profession_by_id($name) {
		$sql = "select id,name from profession where name = '".$name."'";
		$query = $this->db->query($sql);
		return $query->row_object();
	}

	function set_profession($data) {
		return $this->db->insert('profession', $data);
	}

	function get_level() {
		$sql = "select id,name from level";
		$query = $this->db->query($sql);
		return $query;
	}

	function save_paper($data) {
		/*$sql = "insert into paper (school_id,level_id,file_name,profession_id,file_path,teacher,upload_user,words) value(".
			$data['school'].",".$data['level'].",".$data['file_name'].",".$data['profession'].",".
			$data['file'].",".$data['teacher'].",".$data['upload_user'].",".$data['words'].")";
		
		$query = $this->db->query($sql);*/
		return $this->db->insert($this->table_name, $data);
	}
	
	function update_paper($file_id, $data) {
		/*$sql = "replace into paper (school_id,level_id,file_name,profession_id,file_path,teacher,upload_user,words,file_id) value(".
			$data['school'].",".$data['level'].",".$data['file_name'].",".$data['profession'].",".
			$data['file'].",".$data['teacher'].",".$data['upload_user'].",".$data['words'].",".$data['file_id'].")";
		
		$query = $this->db->query($sql);*/
		return $this->db->update($this->table_name, $data, array('file_id' => $file_id));
	}

	function unlock_paper($id,$user) {
		$sql = "update paper set lock_state=\"unlock\",lock_time=\"\" where file_id=$id";
		$query = $this->db->query($sql);
	}

	function lock_paper($user, $use_school,$lock_time,$download_time,$id) {
		$sql = "update paper set download_user='$user',lock_state=\"lock\",use_school='".$use_school."',lock_time='".$lock_time."',download_time='".$download_time."' where file_id=$id";
		log_message('debug','lock_paper---sql---'.$sql);
		$query = $this->db->query($sql);
		// log_message("debug","lock_paper:".json_encode($data));
		// return $this->db->update($this->table_name, $data, array('file_id' => $file_id));
	}

    function add_download_count($id) {
        $sql = "update paper set count = count+1 where file_id=$id";
        $query = $this->db->query($sql);
    }
	
	function unlock_timeout()
	{
		$papers = $this->db->select()->from($this->table_name)->where("lock_time < '".date("Y-m-d H:i:s",time())."' and lock_state = 'lock'")->order_by("upload_time", "desc")->get()->result_array();
		if($papers)
		{
			foreach($papers as $paper)
			{
				$data = array('lock_time' => '', 'lock_state' => 'unlock');
				$this->db->update($this->table_name, $data, array('file_id' => $paper['file_id']));
			}
		}
	}
}
?>
